import axios from 'axios'
import baseUrl from '../../util/url.js'
const url = baseUrl + 'api.php/records/detail'
const url1 = baseUrl + 'api.php/records/shopcar'
export default {
    namespaced: true,
    state: {
        list: [],
        count:0,
        shoplist:[]
    },
    mutations: {},
    actions: {
        getlist({ state, commit, dispatch }, id) {
            return new Promise((resolve, reject) => {
                axios({
                    method: 'get',
                    url,
                    params: { order: 'id,desc', filter:'id,eq,'+id}
                }).then(res => {
                    state.list = res.data.records
                    resolve(res.data.records)
                }).catch(e => {
                    reject()
                })
            });
        },
        getShopcar({ state, commit, dispatch },id) {
            return new Promise((resolve, reject) => {
                axios({
                    method: 'get',
                    url:url1,
                    params: { transform: 1 ,filter:'uid,eq,'+id}
                }).then(res => {
                    state.shoplist = res.data.records
                    
                    resolve()
                }).catch(e => {
                    reject()
                })
            });
        },
        updateShop({ state, commit, dispatch },obj) {
            obj.goodsid = obj.goodsid*1
            //price和num更改了数据类型 将varchar改为 int
            console.log(obj)
            return new Promise((resolve, reject) => {
                axios({
                    method: 'put',
                    url:url1+'/' + obj.id,
                    data:{
                        num:obj.num
                    }
                }).then(res => {
                    resolve()
                }).catch(e => {
                    console.log(e)
                    reject()
                })
            });
        },
        postShop(context,obj) {
            return new Promise((resolve, reject) => {
                axios({
                    method: 'post',
                    url:url1,
                    data:obj
                }).then(res => {
                    resolve(res.data)
                }).catch(e => {
                    reject()
                })
            });
        },
        saveShop({ state, commit, dispatch }, item) {
            return new Promise((resolve, reject) => {
                axios({
                    method: 'post',
                    url:url1,
                    data:item
                }).then(res => {
                    resolve()
                }).catch(e => {
                    reject()
                })
            });
        },
        getShopNum({ state, commit, dispatch }) {
            return new Promise((resolve, reject) => {
                axios({
                    method: 'get',
                    url:url1,
                    params: { transform: 1 ,}
                }).then(res => {
                    let detailarr = res.data.records
                    let arr=0
                    for(var i =0;i<detailarr.length;i++){
                        arr += detailarr[i].num*1
                    }
                    
                    state.count = arr
                }).catch(e => {
                    reject()
                })
            });
        },
        
    }

}